﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class UnitBehaviour : MonoBehaviour {

    public Unit Unit;

    public static Material DefaultMaterial
    {
        get
        {
            if (defaultMat == null)
            {
                defaultMat = new Material(Shader.Find("Sprites/Default"));
                defaultMat.color = Color.white;
            }

            return defaultMat;
        }
    } static Material defaultMat;

    public virtual void Init()
    {
    }

    public virtual void Clear()
    {
        foreach (var go in childGos)
            Destroy(go);

        childGos.Clear();
    }

    protected List<GameObject> childGos = new List<GameObject>();
    protected GameObject NewChildObj(string name)
    {
        var go = new GameObject("edge");
        childGos.Add(go);
        go.transform.SetParent(transform, false);
        return go;
    }

    public virtual void Refresh()
    {
        Clear();
        Init();
    }
	
	// Update is called once per frame
	void Update () {
        if (Unit == null)
            return;

        transform.localPosition = new Vector3(Unit.Pos.x, Unit.Pos.y, 0);
        transform.localRotation = Quaternion.Euler(0, 0, MathEx.Arc2Dir(Unit.Angle));
	}
}
